<!--
 * @Description: 公司/分公司业主确认延期佐证资料
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2023-03-02 15:42:48
 * @LastEditTime: 2023-03-03 09:22:59
 * @FilePath: \cscec-pms-admin\src\views\project\performanceAnalysis\branchPerformanceAnalysis\components\supportingInformationFile.vue
-->

<template>
  <div>
    <a-table bordered rowKey="fileId" :columns="columns" :data-source="tableData">
    <span slot="action" slot-scope="text, record">
        <div class="pub-table-list-action">
          <span v-if="!checkVisibleInDetail">
            <a href="javascript:" @click="$noticeWarning('抱歉，app端无法导出，请在系统电脑端进行导出')">导出</a>
          </span>
          <template v-else>
            <span v-if="record.fileInfoVO">
              <a :href="'/api/ycloud-file-center/oper/download?access_token=' + token + '&fileId=' + record.fileInfoVO.fileId" target="_parent">导出</a>
            </span>
            <span v-if="record.fileInfoVO" target="_blank" @click="$filePreview(record.fileInfoVO)">预览</span>
          </template>
        </div>
      </span>
  </a-table>
  </div>
</template>
<script>
import { supportingInformationFileColumns } from '../model.js' 
import { checkVisibleInDetail } from '@/utils/util'
import Vue from 'vue'
export default {
  name: 'supportingInformationFile',
  components: {},
  props:{
    // 附件列表
    data:{
      type: Array,
      default: ()=>[]
    },
    // 是否分公司
    useBranch:{
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      checkVisibleInDetail: checkVisibleInDetail(this),
      token: Vue.getAcloudProvider().getAccessToken(),
      // 表头
      columns: this.useBranch ? supportingInformationFileColumns.filter(el=> el.dataIndex !== 'branchCompanyName'): supportingInformationFileColumns,
      // 表数据
      tableData: this.$clone(this.data)
    }
  },
  mounted(){
  },
  watch:{
    data:{
      handler(val){
        if(val && val.length>0){
          if(!this.useBranch){
            this.combineRow('branchCompanyName')
          }
          //console.log(val)
          this.combineRow('name')
        }
      },
      immediate:true
    }
  },
  methods:{
    /**
     * @description: 找出合并行的索引并赋值合并
     * @param {string} key 需要合并的字段名
     * @return {void}
     */    
    combineRow(key) {
      const tableData = this.tableData
      for (let i = 0; i < tableData.length; i++) {
        const item = tableData[i]
        let count = 1
        for (let j = i + 1; j < tableData.length; j++) {
          // 如果是同一个值，往后递增
          if (item[key] === tableData[j][key]) {
            count++
            // 往后相同的值都设为空单元格
            tableData[j][`${key}RowSpan`] = 0
            // 只有同值第一个才设置合并的单元格数
            item[`${key}RowSpan`] = count
            // 所有都是为同一个值的情况
            // 如果到了尾部，则循环结束
            if (j === tableData.length - 1) {
                return
            }
          } else {
            // 指针跳转到下一个，从下一排开始
            i = j - 1
            count = 1
            tableData[j][`${key}RowSpan`] = 1
            break
          }
        }
      }
      this.tableData = tableData
    }
  }
}
</script>
<style lang="less" scoped>
</style>
